package com.yudean.esb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yudean.esb.domain.sys.AppUser;
import com.yudean.esb.security.AppUserService;
import com.yudean.esb.util.JsonModel;
import com.yudean.esb.util.SecurityTools;
import com.yudean.esb.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户管理
 */
@Controller
@RequestMapping("")
public class AppUserController {

    @Resource
    private AppUserService appUserService;

    /**
     * 访问用户管理界面
     * @return
     */
    @GetMapping("/users/user")
    public String index(){
        return "user/user";
    }

    /**
     * 根据id查找
     */
    @GetMapping("/users/getUser")
    @ResponseBody
    public JsonModel getById(Integer id){
        return new JsonModel(true,appUserService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/users/{currentPage}/{pageSize}")
    @ResponseBody
    public JsonModel getPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize,AppUser appUser){
        IPage<AppUser> page = appUserService.getPage(currentPage, pageSize,appUser);
        //如果当前页码值大于总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if(currentPage > page.getPages()){
            page = appUserService.getPage( (int)page.getPages(), pageSize,appUser);
        }
        return new JsonModel(true,page);
    }

    /**
     * 修改
     */
    @PutMapping("/users")
    @ResponseBody
    public JsonModel update(@RequestBody AppUser appUser){
        final int count = appUserService.updateSysAppUser(appUser);
        return new JsonModel(count > 0,appUser);
    }


    //查询最大id值
    private int selectMaxId(){
        return appUserService.selectMaxId();
    }

    /**
     * 新增
     */
    @PostMapping("/users")
    @ResponseBody
    public JsonModel save(@RequestBody AppUser user){
        if(user.getState()==null) user.setState(1);
        if (StringUtil.isEmpty(user.getPassword())) user.setPassword("123456");     //设置默认密码
        user.setLoginId(selectMaxId()+1);
        user.setPassword(StringUtil.encodePassword(user.getPassword(),"SHA"));
        boolean flag = appUserService.createUser(user)>0;
        return new JsonModel(flag,flag?"添加成功^_^":"添加失败-_-");
    }

    /**
     * 访问创建token界面
     * @return
     */
    @RequestMapping("/createToken")
    public String token(){
        return "user/createToken";
    }

    /**
     * 生成token
     * @param userId
     * @param expireTime
     * @return
     */
    @PostMapping("/users/getToken")
    @ResponseBody
    public JsonModel getToken(String userId, String expireTime){
        String token = SecurityTools.wrapUserInfo(userId, Long.parseLong(expireTime));
        JsonModel jsonModel = new JsonModel();
        if(!StringUtil.isEmpty(token)){
            jsonModel.setResult(true);
            jsonModel.setMsg("Token生成成功");
            jsonModel.setData(token);
        }else{
            jsonModel.setResult(false);
            jsonModel.setMsg("Token生成失败。userId:"+userId+",expireTime:"+expireTime);
        }
        return jsonModel;
    }

}
